package nl.ajonker.railroad.ui;

import nl.ajonker.railroad.bl.NetworkSwitchService;
import nl.ajonker.railroad.dl.SwitchRail;
import nl.ajonker.railroad.dl.Track;
import nl.ajonker.railroad.pl.RailView;
import nl.ajonker.railroad.pl.TrackView;
import nl.ajonker.railroad.sl.SwitchListener;
import nl.ajonker.railroad.sl.SwitchService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Collection;

public class RailRoad {
    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(final String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    final RailRoad window = new RailRoad();
                    window.frame.setVisible(true);
                } catch (final Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public RailRoad() {
        frame = new JFrame();
        frame.setTitle("NOLS");
        frame.setBounds(100, 100, 770, 320);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new BorderLayout());
        // http://curvefever.com/content/locomotive
        frame.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/loco.jpg")));

        Point start = new Point(20, 50);
        int dimension = 50;
        Track track = new Track();
        TrackView trackView = new TrackView(track, start, dimension);
        JLogPanel logPanel = new JLogPanel();
        final SwitchService switchService = new NetworkSwitchService(logPanel);
        track.addListener(new SwitchListener() {
            @Override
            public boolean onSwitch(SwitchRail rail) {
                try {
                    switchService.performSwitch(rail);
                } catch (IOException e) {
                    return false;
                }
                return true;
            }
        });

        JPanel buttonPanel = createButtonPanel(trackView);

        frame.add(buttonPanel, BorderLayout.WEST);
        frame.add(new NetworkPanel(switchService), BorderLayout.NORTH);
        frame.add(new JTrackPanel(start, trackView, dimension), BorderLayout.CENTER);
        frame.add(logPanel, BorderLayout.SOUTH);

        switchService.initialize(track.getRails());
    }

    private JPanel createButtonPanel(final TrackView trackView) {
        Collection<RailView> rails = trackView.getRailViews();
        JPanel buttonPanel = new JPanel(new GridLayout(rails.size(), 1));
        for (final RailView railView : rails) {
            Icon icon = getRailIcon(railView);
            final JButton jButton = new JButton("Wissel " + railView.getName(), icon);
            jButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    trackView.switchRailPosition(railView.getName());
                    jButton.setIcon(getRailIcon(railView));
                    frame.repaint();
                }
            });
            buttonPanel.add(jButton);
        }
        return buttonPanel;
    }

    private Icon getRailIcon(RailView railView) {
        if (railView.getType() == RailView.SwitchType.RIGHT) {
            if (railView.getSwitchState() == SwitchRail.SwitchState.STRAIGHT)
                return new ImageIcon(this.getClass().getResource("/right-green.png"));
            else
                return new ImageIcon(this.getClass().getResource("/right-red.png"));
        } else {
            if (railView.getSwitchState() == SwitchRail.SwitchState.STRAIGHT)
                return new ImageIcon(this.getClass().getResource("/left-green.png"));
            else
                return new ImageIcon(this.getClass().getResource("/left-red.png"));
        }
    }
}
